package View.apply;

import Service.DormService;
import Service.RepairService;
import Service.ServiceImpl.DormServiceImpl;
import Service.ServiceImpl.RepairServiceImpl;
import Service.ServiceImpl.systemMsgServiceImpl;
import Service.systemMsgService;
import domain.Dorm;
import domain.Repair;
import domain.User;
import domain.systemMsg;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

public class RepairApplyApprove extends JFrame {
    public RepairApplyApprove(Repair repair, User user){
        init();
        componentInit(repair,user);
    }

    public void init(){
        this.setTitle("维修申请审批");
        this.setSize(448,400);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }
    public void componentInit(Repair repair, User user){
        //------------------JLabel--------------------
        JLabel label1= new JLabel();
        label1.setText("宿舍号:");
        label1.setBounds(97,22,65,30);
        JLabel label2= new JLabel();
        label2.setText("问题:");
        label2.setBounds(99,75,65,30);
        JLabel label3= new JLabel();
        label3.setText("问题描述：");
        label3.setBounds(99,131,65,30);
        JLabel label4= new JLabel();
        label4.setText("审核人:");
        label4.setBounds(101,192,65,30);

        //------------------JTextField--------------------
        JTextField field1= new JTextField();
        field1.setText(repair.getDormnum());
        field1.setBorder(new LineBorder(Color.lightGray));
        field1.setEnabled(false);
        field1.setBounds(180,22,120,30);
        JTextField field2= new JTextField();
        field2.setText(repair.getProblem());
        field2.setBorder(new LineBorder(Color.lightGray));
        field2.setEnabled(false);
        field2.setBounds(183,76,120,30);
        JTextField field3= new JTextField();
        field3.setText(repair.getDescription());
        field3.setBorder(new LineBorder(Color.lightGray));
        field3.setEnabled(false);
        field3.setBounds(183,132,120,30);
        JTextField field4= new JTextField();
        field4.setText("");
        field4.setBorder(new LineBorder(Color.lightGray));
        field4.setBounds(184,194,120,30);

        //------------------JButton--------------------
        JButton button1= new JButton();
        button1.setText("通过");
        button1.setBounds(36,284,95,27);
        JButton button2= new JButton();
        button2.setText("退出");
        button2.setBounds(271,285,95,27);





        this.add(label1);
        this.add(label2);
        this.add(label3);
        this.add(label4);
        this.add(field1);
        this.add(field2);
        this.add(field3);
        this.add(field4);
        this.add(button1);
        this.add(button2);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                RepairService service = new RepairServiceImpl();
                service.updateIsFinish(repair.getId(),field4.getText(),"true");

                //通过维修申请后把申请通过的消息传递给改宿舍的宿舍长
                String dormnum = repair.getDormnum();
                DormService service1 = new DormServiceImpl();
                Dorm dorm = service1.selectDormByDormnum(dormnum);

                String head = dorm.getHead();
                systemMsg msg = new systemMsg();
                msg.setUsername(head);
                msg.setTitle("维修审批已通过");
                msg.setNeirong("宿舍"+dormnum+"你的维修申请已通过,请等待维修人员上门维修");
                msg.setSendTime(new Date());
                msg.setIsread("false");
                systemMsgService service2 = new systemMsgServiceImpl();
                service2.addMsg(msg);

                JOptionPane.showMessageDialog(new JFrame(),"审批完成!");
                dispose();
                new RepairApply(user).setVisible(true);
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new RepairApply(user).setVisible(true);
            }
        });
    }
}
